package com.vision.config;

import jakarta.servlet.http.HttpSession;
import jakarta.websocket.HandshakeResponse;
import jakarta.websocket.server.HandshakeRequest;
import jakarta.websocket.server.ServerEndpointConfig;
import org.springframework.context.annotation.Configuration;

/**
 * 用于获取 websocket 连接中的 httpsession 对象
 * 需要在 websocket 配置类中配置 ： @ServerEndpoint(value = "/websocket", configurator = GetUserInfo.class)
 */
@Configuration
public class GetUserInfo extends ServerEndpointConfig.Configurator {
    // 重写 modifyHandshake 方法，将 httpsession 存入 sec 中
    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {

        // 通过 request 获取 httpsession 对象
        HttpSession httpSession = (HttpSession) request.getHttpSession();
        // 给 sec 中存储 session 对象 key 为 session 类名
        sec.getUserProperties().put(HttpSession.class.getName(),httpSession);
    }
}
